package ru.xrm.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

public class AddNewFilmPage extends InternalPage {

	public AddNewFilmPage(PageManager pages) {
		super(pages);
	}

	@FindBy(name = "name")
	private WebElement filmName;

	@FindBy(name = "year")
	private WebElement filmYear;

	@FindBy(name = "imbdid")
	private WebElement filmImbdbId;

	@FindBy(name = "aka")
	private WebElement filmAkaName;

	@FindBy(name = "duration")
	private WebElement filmDuration;

	@FindBy(name = "rating")
	private WebElement filmRating;

	@FindBy(name = "format")
	private WebElement filmFormat;

	@FindBy(name = "submit")
	private WebElement submitButton;
	
	@FindBy(name = "notes")
	private WebElement filmNotes;
	
	@FindBy(xpath = "//label[@class='error']")
	private List<WebElement> addFilmErrorList;
	

	public AddNewFilmPage setFilmName(String text) {
		filmName.sendKeys(text);
		return this;
	}

	public String getFilmName() {
		return filmName.getAttribute("value");
	}

	public String getFilmYear() {
		return filmYear.getAttribute("value");
	}
	
	public AddNewFilmPage setFilmYear(String text) {
		filmYear.sendKeys(text);
		return this;
	}

	public AddNewFilmPage setFilmImbdbId(String text) {
		filmImbdbId.sendKeys(text);
		return this;
	}

	public AddNewFilmPage setFilmRating(String text) {
		filmRating.sendKeys(text);
		return this;
	}

	public AddNewFilmPage setFilmFormat(String text) {
		filmFormat.sendKeys(text);
		return this;
	}
	
	public AddNewFilmPage setFilmNotes(String test) {
		filmNotes.sendKeys(test);
		return this;
	}
	
	public int getCountAddFilmErrors()
	{
		addFilmErrorList = driver.findElements(By.xpath("//label[@class='error']"));
		int visibleErrors = 0;
		for(WebElement webElement : addFilmErrorList)
		{
			if (webElement.isDisplayed())
			{
				visibleErrors++;
			}			
		}			
		return visibleErrors;
	}

	public void clickSubmitButton() 
	{
		submitButton.click();
	}

	public AddNewFilmPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.xpath("//*[@id='content']//div[@class='addmovie']")));
		return this;
	}
}
